package com.icss.mqsp.data;

import com.icss.mqsp.builder.IBuilder;
import com.icss.mqsp.config.ConstantConfig;
import com.icss.mqsp.util.RequestCondition;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.sf.json.JSONObject;

public class CacheReportBuilder
  implements IBuilder
{
  public JSONObject exec(RequestCondition rc)
  {
    String[] pids = getPids();
    
    Map<String, String> conditionMap = rc.getConditionMap();
    String params = rc.getPamams();
    for (int i = 0; i < pids.length; i++)
    {
      JSONObject jobj = ReportBuilder.buildPageData(pids[i], conditionMap, params);
      
      createFile(jobj, pids[i]);
    }
    String compressFilePath = zipFiles(ConstantConfig.TXT_DIR);
    


    String pubTime = "2016-04-28";
    JSONObject jsonPath = new JSONObject();
    jsonPath.put("path", compressFilePath);
    jsonPath.put("pubTime", pubTime);
    return jsonPath;
  }
  
  private void createFile(JSONObject data, String txtName)
  {
    FileOutputStream fos = null;
    File txtDir = new File(ConstantConfig.TXT_DIR);
    if (!txtDir.exists()) {
      txtDir.mkdirs();
    }
    File txtFile = new File(ConstantConfig.TXT_DIR + ConstantConfig.FILE_SEPARATOR + txtName + ".txt");
    try
    {
      fos = new FileOutputStream(txtFile);
      fos.write(data.toString().getBytes("UTF-8"));
      fos.flush();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      try
      {
        fos.close();
      }
      catch (IOException e1)
      {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        fos.close();
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
  }
  
  private String zipFiles(String filePath)
  {
    String zipFilePath = ConstantConfig.ZIP_DIR + ConstantConfig.FILE_SEPARATOR + ConstantConfig.ZIP_NAME;
    File zipDir = new File(ConstantConfig.ZIP_DIR);
    if (!zipDir.exists()) {
      zipDir.mkdirs();
    }
    try
    {
      File file = new File(filePath);
      File zipFile = new File(zipFilePath);
      InputStream input = null;
      ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
      if (file.isDirectory())
      {
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
          if ((!files[i].getName().equals(".DS_Store")) && (!files[i].getName().equals(ConstantConfig.ZIP_NAME)))
          {
            input = new FileInputStream(files[i]);
            zipOut.putNextEntry(new ZipEntry(file.getName() + ConstantConfig.FILE_SEPARATOR + files[i].getName()));
            int temp = 0;
            while ((temp = input.read()) != -1) {
              zipOut.write(temp);
            }
            input.close();
          }
        }
      }
      zipOut.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return ConstantConfig.ZIP_NAME;
  }
  
  private String[] getPids()
  {
    return new String[] { "INDEX_001", "INDEX_001_DETAIL" };
  }
}
